#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
调试坐标传递和转换
"""

def debug_coordinate_flow():
    """调试坐标流程"""
    
    print("=== 坐标系统调试 ===")
    
    # 模拟前端发送的数据
    frontend_data = {
        'regions': {
            '0': {
                'elements': [{
                    'image_url': 'https://example.com/test.png',
                    'user_placement_data': {
                        'position': {'x': 1500, 'y': 2500},  # 前端发送的坐标
                        'dimension': {'width': 400, 'height': 400},
                        'scale': {'scale_x': 1.0, 'scale_y': 1.0},
                        'angle_of_rotation': 30
                    }
                }]
            }
        }
    }
    
    # 模板信息
    template_info = {
        'width': 9314,   # 模板实际尺寸
        'height': 5663
    }
    
    print(f"模板尺寸: {template_info['width']} x {template_info['height']}")
    
    # 分析坐标
    for region_index, region_data in frontend_data['regions'].items():
        for element_idx, element in enumerate(region_data['elements']):
            user_data = element['user_placement_data']
            position = user_data['position']
            dimension = user_data['dimension']
            
            print(f"\\n元素 {element_idx + 1}:")
            print(f"  前端发送坐标: ({position['x']}, {position['y']})")
            print(f"  前端发送尺寸: {dimension['width']} x {dimension['height']}")
            print(f"  旋转角度: {user_data['angle_of_rotation']}°")
            
            # 检查坐标是否合理
            x_percent = position['x'] / template_info['width'] * 100
            y_percent = position['y'] / template_info['height'] * 100
            
            print(f"  相对位置: X={x_percent:.1f}%, Y={y_percent:.1f}%")
            
            # 检查是否在画布范围内
            if 0 <= position['x'] <= template_info['width'] and 0 <= position['y'] <= template_info['height']:
                print(f"  ✅ 坐标在画布范围内")
            else:
                print(f"  ⚠️  坐标超出画布范围")
                
            # Photoshop坐标系说明
            print(f"  PS坐标: ({position['x']}, {position['y']}) - 左上角为原点")

def test_coordinate_conversion():
    """测试坐标转换"""
    print("\\n=== 坐标转换测试 ===")
    
    # 假设的前端显示信息
    display_info = {
        'canvas_display_width': 800,    # 前端Canvas显示宽度
        'canvas_display_height': 486,   # 前端Canvas显示高度 (800 * 5663/9314)
        'template_actual_width': 9314,  # 模板实际宽度
        'template_actual_height': 5663  # 模板实际高度
    }
    
    # 计算显示缩放比例
    scale_x = display_info['canvas_display_width'] / display_info['template_actual_width']
    scale_y = display_info['canvas_display_height'] / display_info['template_actual_height']
    display_scale = min(scale_x, scale_y)
    
    print(f"显示缩放比例: {display_scale:.4f}")
    print(f"ScaleX: {scale_x:.4f}, ScaleY: {scale_y:.4f}")
    
    # 测试坐标转换
    test_cases = [
        {'canvas_x': 100, 'canvas_y': 100, 'desc': '前端Canvas坐标'},
        {'canvas_x': 400, 'canvas_y': 200, 'desc': '前端Canvas中心附近'},
    ]
    
    for case in test_cases:
        canvas_x = case['canvas_x']
        canvas_y = case['canvas_y']
        
        # 转换为模板坐标
        template_x = canvas_x / display_scale
        template_y = canvas_y / display_scale
        
        print(f"\\n{case['desc']}:")
        print(f"  Canvas坐标: ({canvas_x}, {canvas_y})")
        print(f"  模板坐标: ({template_x:.0f}, {template_y:.0f})")
        print(f"  这就是发送给后端的坐标")

if __name__ == '__main__':
    debug_coordinate_flow()
    test_coordinate_conversion()